﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Bank : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Trace.Write("Function call start");

        lblValue.Text = GetFileContaint();
        //var roll = Request.Params["rollno"];
        //if (roll.Equals("100"))
        //    lblValue.Text = "Roll No. 100 " + DateTime.Now;
        //else
        //    lblValue.Text = "Roll No." + roll + "  " + DateTime.Now;

        Trace.Write("Function call end");


        var fileList = Directory.GetFiles(Server.MapPath("uploadfiles"));
        List<FileInformation> fileInfo = new List<FileInformation>();
        foreach (var filePath in fileList)
        {
            int filePos = filePath.LastIndexOf("\\") + 1;
            string fName = filePath.Substring(filePos);

            fileInfo.Add(new FileInformation { FileName = fName, FilePath = filePath });
        }



    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string url = "Tran.aspx?";
        url += "ID=10" + "&";
        url += "Name=" + Server.UrlEncode("http://localhost/SecurityWebUI/Library.aspx");
        //string url2 = Server.UrlEncode("");
        Response.Redirect(url);

    }
    protected void btnshow_Click(object sender, EventArgs e)
    {
     //   lblValue.Text = "Hi " + lblTextBox.Text;
    }
    protected void btnupload_Click(object sender, EventArgs e)
    {
        var filePath = fileUpload.PostedFile.FileName;
        var filecontentStream = fileUpload.PostedFile.InputStream;

        int filePos = filePath.LastIndexOf("\\") + 1;
        string fileName = filePath.Substring(filePos);

        Guid guid = Guid.NewGuid();
        string fileGuidName = guid.ToString() + "_" + fileName;

        string serverFolder = Server.MapPath("uploadfiles");
        if (!Directory.Exists(serverFolder))
            Directory.CreateDirectory(serverFolder);

        string serverFilePath = Path.Combine(serverFolder, fileGuidName);
        FileStream fs = null;
        try
        {
            fs = File.Create(serverFilePath);

            byte[] buffer = new byte[1024];
            int read = 0;
            while ((read = filecontentStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                fs.Write(buffer, 0, read);
            }
        }
        finally
        {
            if (fs != null)
            {
                fs.Close();
                fs.Dispose();
            }
            if (filecontentStream != null)
            {
                filecontentStream.Close();
                filecontentStream.Dispose();
            }
        }
    }
    protected void btnDownload_Click(object sender, EventArgs e)
    {
        var fileName = Path.Combine(Server.MapPath("uploadfiles"), "827535f3-69da-46be-8c3a-ec5f9d4d71f9_CustomControlProjects.zip");
        HttpContext.Current.Response.Clear();
        Response.ContentType = "application/binary";
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
        // HttpContext.Current.Response.BinaryWrite(fileBytes);
        //Response.AppendHeader("Content-Length", fileBytes.Length.ToString());
        HttpContext.Current.Response.End();

    }

    private string GetFileContaint()
    {
        for (int i = 0; i < 99999999; i++)
        {

        }
        var fileText = (string)HttpRuntime.Cache.Get("list");

        if (fileText == null)
        {
            fileText = System.IO.File.ReadAllText(@"C:\Movies\list.txt");
            HttpRuntime.Cache.Add("list", fileText, new System.Web.Caching.CacheDependency(@"C:\Movies\list.txt"), DateTime.Now.AddSeconds(20),
                    TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
        }
        return fileText;
    }
}
class FileInformation
{
    public string FileName { get; set; }
    public string FilePath { get; set; }
}

